package cn.itsource.ymcc.web.controller;

import cn.itsource.ymcc.context.LoginContext;
import cn.itsource.ymcc.service.ICourseCartService;
import cn.itsource.ymcc.domain.CourseCart;
import cn.itsource.ymcc.query.CourseCartQuery;
import cn.itsource.ymcc.result.JSONResult;
import cn.itsource.ymcc.result.PageList;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/courseCart")
public class CourseCartController {

    @Autowired
    public ICourseCartService courseCartService;


    /**
     * 删除购物车
     */
    @RequestMapping(value="/remove",method= RequestMethod.POST)
    public JSONResult remove(@RequestBody List<Long> courseIds){
        courseCartService.remove(courseIds);
        return JSONResult.success( );
    }
    /**
     * 计算价格
     */
    @RequestMapping(value="/calAmount",method= RequestMethod.POST)
    public JSONResult calAmount(@RequestBody List<Long> cartIds){
        return JSONResult.success( courseCartService.calAmount(cartIds));
    }

    /**
     * 查询用户的购物车
     */
    @RequestMapping(value="/listForUser",method= RequestMethod.GET)
    public JSONResult listForUser(){
        return JSONResult.success( courseCartService.listForUser());
    }



    /**
     * 用户添加购物车
     */
    @RequestMapping(value="/add/{courseId}",method= RequestMethod.POST)
    public JSONResult add(@PathVariable("courseId") Long courseId){
        courseCartService.add(courseId);
        return JSONResult.success();
    }


    /**
    * 删除对象
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public JSONResult delete(@PathVariable("id") Long id){
        courseCartService.deleteById(id);
        return JSONResult.success();
    }

    /**
   * 获取对象
   */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public JSONResult get(@PathVariable("id")Long id){
        return JSONResult.success(courseCartService.selectById(id));
    }


    /**
    * 查看所有
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public JSONResult list(){
        return JSONResult.success(courseCartService.selectList(null));
    }


    /**
    * 分页查询数据
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public JSONResult json(@RequestBody CourseCartQuery query){
        Page<CourseCart> page = new Page<CourseCart>(query.getPage(),query.getRows());
        page = courseCartService.selectPage(page);
        return JSONResult.success(new PageList<CourseCart>(page.getTotal(),page.getRecords()));
    }
}
